package com.dayatang.hrm.compensation.vo;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayatang.commons.domain.DataType;
import com.dayatang.commons.domain.Value;
import com.dayatang.hrm.compensation.CompensationConstant;

public class RealValue extends Value {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6107996442548389328L;

	private static final Logger logger = LoggerFactory
			.getLogger(RealValue.class);

	public static String DATE_FORMAT_PATTERN = CompensationConstant.DATE_FORMAT_PATTERN;

	public static String DATE_FORMAT_FULL_PATTERN = CompensationConstant.DATE_FORMAT_FULL_PATTERN;

	public Object getRealValue() {
		return null;
	}

	public static RealValue valueOf(Object obj) {

		RealValue realValue = null;

		if (obj instanceof String) {
			realValue = new RealStringValue();
			realValue.setDataType(DataType.STRING);
			realValue.setValue((String) obj);
		} else if (obj instanceof Integer) {
			realValue = new RealIntValue();
			realValue.setDataType(DataType.INT);
			realValue.setValue(((Integer) obj).toString());
		} else if (obj instanceof Long) {
			realValue = new RealLongValue();
			realValue.setDataType(DataType.LONG);
			realValue.setValue(((Long) obj).toString());
		} else if (obj instanceof Double) {
			realValue = new RealDoubleValue();
			realValue.setDataType(DataType.DOUBLE);
			realValue.setValue(((Double) obj).toString());
		} else if (obj instanceof BigDecimal) {
			realValue = new RealBigDecimalValue();
			realValue.setDataType(DataType.BIG_DECIMAL);
			realValue.setValue(((BigDecimal) obj).toString());
		} else if (obj instanceof Boolean) {
			realValue = new RealBooleanValue();
			realValue.setDataType(DataType.BOOLEAN);
			realValue.setValue(((Boolean) obj).toString());
		} else if (obj instanceof Date) {
			realValue = new RealDateValue();
			realValue.setDataType(DataType.DATE);
			realValue.setValue(DateFormatUtils.format((Date) obj,
					DATE_FORMAT_PATTERN));
		} else if (obj instanceof Time) {
			realValue = new RealTimeValue();
			realValue.setDataType(DataType.TIME);
			realValue.setValue(((Time) obj).toString());
		} else if (obj instanceof Timestamp) {
			realValue = new RealTimestampValue();
			realValue.setDataType(DataType.DATE_TIME);
			realValue.setValue(DateFormatUtils.format(((Timestamp) obj)
					.getTime(), DATE_FORMAT_FULL_PATTERN));
		} else {
			if (logger.isWarnEnabled()) {
				logger.warn("不支持的类型转换：【{}】。", obj.getClass().toString());
				RealValue r = (RealValue) obj;
				logger.warn(r.getValue());
			}
			throw new RuntimeException("不支持的类型转换。");
		}

		return realValue;

	}
}
